﻿using System;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using TimeTravel.Components;
using TimeTravel.Extentions;
using TimeTravel.IData;

namespace TimeTravel.SqlServer  
{
    public class ClientInfoData : IClientInfoData
    {
        #region Fields
        
        private static readonly SqlHelper DbHelper = new SqlHelper();
        private static readonly string[] Columns = {"[id]", "[name]", "[picture]", "[linkurl]"};

        #endregion

        #region Implementation of IDataBase<ClientInfo>

        /// <summary>
        /// 添加记录
        /// </summary>
        public int Add(ClientInfo client)
        {
            if (client == null) return 0;
            var strSql = new StringBuilder();
            strSql.Append("insert into ClientInfo(");
            strSql.Append("[name],[picture],[linkurl]) ");
            strSql.Append(" values ");
            strSql.Append("(@name,@picture,@linkurl);");
            strSql.Append("Select @@IDENTITY;");
            SqlParameter[] parameter = {
                                           new SqlParameter("@name", SqlDbType.NVarChar, 50),
                                           new SqlParameter("@picture", SqlDbType.NVarChar, 50),
                                           new SqlParameter("@linkurl", SqlDbType.NVarChar, 50)
                                       };
            parameter[0].Value = client.Name;
            parameter[1].Value = client.Picture;
            parameter[2].Value = client.LinkUrl;
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameter);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            if (id == 0) return;
            var strSql = new StringBuilder();
            strSql.Append("update ClientInfo set DeleteFlag = 1 where [id]=@id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="clientInfo"></param>
        public void Update(ClientInfo clientInfo)
        {
            if (clientInfo == null || clientInfo.ID == 0) return;
            var strSql = new StringBuilder();
            strSql.Append("update ClientInfo set ");
            strSql.Append("[name] = @name,");
            strSql.Append("[picture] = @picture,");
            strSql.Append("[linkurl] = @linkurl ");
            strSql.Append("where [id] = @id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@name", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@picture", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@linkurl", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = clientInfo.Name;
            parameters[1].Value = clientInfo.Picture;
            parameters[2].Value = clientInfo.LinkUrl;
            parameters[3].Value = clientInfo.ID;
            DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public IList<ClientInfo> GetAll()
        {
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from ClientInfo ", String.Join(",", Columns));
            strSql.Append("where DeleteFlag = 0;");
            var result = DbHelper.ExecuteObjectList(strSql.ToString(), DataReaderParser);
            return result.ToList();
        }

        /// <summary>
        /// 根据条件获取
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RecordSet<ClientInfo> GetBySearch(PageQueryInfo query)
        {
            if(query == null) return new RecordSet<ClientInfo>();
            return DbHelper.ExecutePaging("ClientInfo", query, DataReaderParser, Columns);
        }

        /// <summary>
        /// 获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ClientInfo GetOne(int id)
        {
            if (id == 0) return null;
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from ClientInfo ", String.Join(",", Columns));
            strSql.Append("where [id] = @id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            return DbHelper.ExecuteObject(CommandType.Text, strSql.ToString(), DataReaderParser, parameters);
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public int GetTotalCount()
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(id) from ClientInfo ");
            strSql.Append("where DeleteFlag = 0;");
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
            return result == null ? 0 : Convert.ToInt32(result);
        }

        #endregion

        #region BuildNavMenuObject

        /// <summary>
        /// 从DataReader中构建对象
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private ClientInfo DataReaderParser(IDataReader reader)
        {
            var client = new ClientInfo
                             {
                                 ID = reader.GetInt32("id"),
                                 LinkUrl = reader.GetString("linkurl"),
                                 Picture = reader.GetString("picture"),
                                 Name = reader.GetString("name")
                             };
            return client;
        }

        #endregion
    }
}

